package com.koron.css2.clientCenter.impl;


import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.bean.UserInfoMergeDetail;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMergeDetailMapper;
import com.koron.css2.clientCenter.queryBean.UserInfoMergeDetailQuery;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.BusinessAreaMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 根据证件号码查询相关的用户数据
 *
 * @author 朱超
 * @since 2021-12-24 15:15:08
 */
@Service
public class UserInfoMergeDetailListByCertNo implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
            CtmInfoMapper ctmInfoMapper = factory.getMapper(CtmInfoMapper.class);

            // 1. 根据请求json获取实例bean
            UserInfoMergeDetailQuery userInfoMergeDetailQuery = JsonUtils.objectToPojo(req.getData(), UserInfoMergeDetailQuery.class);
            List<UserInfoMergeDetail> userInfoMergeDetailList = getUserInfoMergeDetailListByCertNo(factory, userInfoMapper, ctmInfoMapper, userInfoMergeDetailQuery);
            // 10. 返回结果
            MessageBean<List> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "根据证件号码查询并户详情成功", List.class);
            msg.setData(userInfoMergeDetailList);
            return msg;
        } catch (Exception e) {
            logger.error("根据证件号码查询并户详情失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "根据证件号码查询并户详情失败", void.class);
        }
    }

    public List<UserInfoMergeDetail> getUserInfoMergeDetailListByCertNo(SessionFactory factory, UserInfoMapper userInfoMapper, CtmInfoMapper ctmInfoMapper, UserInfoMergeDetailQuery userInfoMergeDetailQuery) {
        UserInfoMergeDetailMapper userInfoMergeDetailMapper = factory.getMapper(UserInfoMergeDetailMapper.class);
        // 3. 查询当前证件号码下的所有用户(查出USER_NO, SETUP_METER_ADDR, CTM_NO, ACCOUNT_NO)
        List<UserInfoMergeDetail> userInfoMergeDetailList = userInfoMergeDetailMapper.getListFromUserInfo(userInfoMergeDetailQuery);
        // 4. 查询当前证件号码下的所有客户编码
        String certNo = userInfoMergeDetailQuery.getCertNo();
        List<String> ctmNoList = userInfoMapper.getCtmNoListByCertNo(certNo);
        // 5. 查询客户编码列表下的所有的账户编码
//        Map<String, List<String>> ctmToAccountMap = new HashMap<>();
//        if (ctmNoList != null && ctmNoList.size() > 0) {
//            List<UserInfoMergeDetail> ctmToAccountList = userInfoMapper.getAccountNoListByCTmList(ctmNoList);
//            // 5.1 根据客户编码聚合账户编码列表
//            ctmToAccountMap = ctmToAccountList.stream().collect(Collectors.groupingBy(
//                    UserInfoMergeDetail::getCtmNo, Collectors.mapping(UserInfoMergeDetail::getAccountNo, Collectors.toList())));
//        }
        // 6. 查询客户对应的用户数量
        Map<String, Map> ctmUserAmountMap = new HashMap<>();
        if (ctmNoList != null && ctmNoList.size() > 0) {
            ctmUserAmountMap = userInfoMapper.getCtmUserAmountByCtmNoList(ctmNoList);
        }
        // 查询pub_business_area中的id和area的对应map
        BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
        Map<String,Map> areaNameIdMap = new HashMap<>();
        areaNameIdMap = businessAreaMapper.getNameAndId();
        for (UserInfoMergeDetail userInfoMergeDetail : userInfoMergeDetailList) {
            String ctmNo = userInfoMergeDetail.getCtmNo();
            // 7. 根据ctm_no查询ctm_name, ctm_addr
            CtmInfoBean ctmInfoBean = ctmInfoMapper.getCtmInfoByCtmNo(userInfoMergeDetail.getCtmNo(), 1);
            userInfoMergeDetail.setCtmAddr(ctmInfoBean.getCtmAddr());
            userInfoMergeDetail.setCtmName(ctmInfoBean.getCtmName());
//            userInfoMergeDetail.setCtmNoList(ctmNoList);
            if (!ctmUserAmountMap.isEmpty()) {
                userInfoMergeDetail.setCtmUserAmount(Integer.parseInt(ctmUserAmountMap.get(ctmNo).get("ctmUserAmount").toString()));
            }
            // 8. 根据account_no查询accountUserAmount
            Integer accountUserAmount = userInfoMapper.getAccountUserAmountByAccountNo(userInfoMergeDetail.getAccountNo());
            userInfoMergeDetail.setAccountUserAmount(accountUserAmount);
            // 9. 根据ctmNo查询当前客户下对应的所有账户
//                List<String> accountNoList = userInfoMapper.getAccountNoListByCtmNo(userInfoMergeDetail.getCtmNo());
//            if (!ctmToAccountMap.isEmpty()) {
//                userInfoMergeDetail.setAccountNoList(ctmToAccountMap.get(ctmNo));
//            }
            // 根据businessArea获取businessAreaName
            String businessArea = userInfoMergeDetail.getBusinessArea();
            Map<String,Object> singleMap = areaNameIdMap.get(businessArea);
            if (singleMap != null && !singleMap.isEmpty()) {
                userInfoMergeDetail.setBusinessAreaName(singleMap.get("area_name").toString());
            }
        }
        return userInfoMergeDetailList;
    }


}
